package com.atguigu.day22.exer.codetest;

import java.util.Scanner;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/28 9:52
 * @注释 编写方法实现：求某年某月某日是这一年的第几天
 */
public class CodeTest02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = 0;
        boolean flag = true;
        l:
        while (flag) {
            System.out.println("请输入年份:");
            year = sc.nextInt();
            if (year < 0) {
                System.out.println("您输入的年份错误");
                continue l;
            }
            flag = false;
        }
        //判断是否是闰年
        boolean years = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
        int month = 0;
        flag = true;
        m:
        while (flag) {
            System.out.println("请输入月份:");
            month = sc.nextInt();
            if (month < 1 || month > 12) {
                System.out.println("您输入的月份有误");
                continue m;
            }
            flag = false;
        }
        int day = 0;
        flag = true;
        d:
        while (flag) {
            System.out.println("请输入日期:");
            day = sc.nextInt();
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                if (day < 0 || day > 31) {
                    System.out.println("您输入的日期有误");
                    continue d;
                }
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day < 0 || day > 30) {
                    System.out.println("您输入的日期有误");
                    continue d;
                }
            } else {
                if (years) {
                    if (day < 0 || day > 29) {
                        System.out.println("您输入的日期有误");
                        continue d;
                    }
                } else {
                    if (day < 0 || day > 28) {
                        System.out.println("您输入的日期有误");
                        continue d;
                    }
                }
            }
            flag = false;
        }
        int days = 0;
        int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//记录每个月的天数
        //将输入月份的前几个月相加
        for (int i = 0; i < month - 1; i++) {
            days += months[i];
        }
        //闰年二月+1天
        if ((month > 2) && years) {
            days++;
        }
        //加上输入月份当月的天数
        days += day;
//        switch (month) {
//            case 12:
//                days += 30;
//            case 11:
//                days += 31;
//            case 10:
//                days += 30;
//            case 9:
//                days += 31;
//            case 8:
//                days += 31;
//            case 7:
//                days += 30;
//            case 6:
//                days += 31;
//            case 5:
//                days += 30;
//            case 4:
//                days += 31;
//            case 3:
//                days += 28;
//                if (years) {
//                    days++;
//                }
//            case 2:
//                days += 31;
//            case 1:
//                days += day;
//                break;
//        }
        System.out.println(year + "年" + month + "月" + day + "日是一年的第" + days + "天");
    }
}
